package com.yaxin.mq.producer;

import com.yaxin.mq.config.RabbitTtlQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author lgj
 * @version 1.0
 * @description 生成者
 * @date 2025/7/18 14:50
 */
@Slf4j
/*@Component*/
public class DelayRabbitProducer {

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendDelayMessage(String message, long delay) {
        rabbitTemplate.convertAndSend(
            RabbitTtlQueueConfig.DELAY_EXCHANGE_NAME,
            RabbitTtlQueueConfig.DELAY_ROUTING_KEY,
            message,
            msg -> {
                msg.getMessageProperties().setDelay((int) delay); // 注意：需要确保 delay 在 int 范围内
                return msg;
            },
            new CorrelationData(UUID.randomUUID().toString())
        );
        log.info("【生产者】Delayed message send: " + message);
    }

}
